13. Cluster Visualization

Cluster Visualization

Now that you have performed the clustering step, and have lists of points for each object (cluster_indices),
you can perform the final step of this exercise, visualizing the results in RViz!

Choosing a unique color for each segmented Object

In order to visualize the results in RViz, you need to create one final point cloud, let's call it "cluster_cloud" of type PointCloud_PointXYZRGB. This cloud will contain points for each of the segmented objects, with each set of points having a unique color.

#Assign a color corresponding to each segmented object in scene
cluster_color = get_color_list(len(cluster_indices))

color_cluster_point_list = []

for j, indices in enumerate(cluster_indices):
    for i, indice in enumerate(indices):
        color_cluster_point_list.append([white_cloud[indice][0],
                                        white_cloud[indice][1],
                                        white_cloud[indice][2],
                                         rgb_to_float(cluster_color[j])])

#Create new cloud containing all clusters, each with unique color
cluster_cloud = pcl.PointCloud_PointXYZRGB()
cluster_cloud.from_list(color_cluster_point_list)

Publishing ros_cluster_cloud

With cluster_cloud created, you are now ready to convert it to ROS' PointCloud2 type and publish it. You can do so using pcl_helper.py's pcl_to_ros() function.

 ros_cluster_cloud = pcl_to_ros(cluster_cloud)

Visualizing the Results in Rviz

To see the results of the segmentation in RViz, you need to create a new publisher and publish your ros_cluster_cloud to it. Follow along with how you created publishers for the table and objects and create a new publisher called /pcl_cluster for the cluster cloud.

Once you've done that, save and run your node, then in RViz simply change the topic dropdown for the PointCloud2 display from /sensor_stick/point_cloud to /pcl_cluster as show in the screenshot.

Great job! You have now completed the segmentation exercise!